package com.tangkun.environment.activity;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.tangkun.environment.R;
import com.tangkun.environment.network.GlobalUrl;
import com.tangkun.environment.utils.JumpHelper;
import com.tangkun.environment.utils.LogUtils;
import com.tangkun.environment.utils.Utils;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 二维码界面
 */
public class ErWeiMaActivity extends BaseActivity {
    @BindView(R.id.iv_erweima)
    ImageView ivErweima;

    private Context mContext;
    /**
     * 二维码文件路径
     */
    private String mErWeiMaFilePath = "";
    /**
     * id
     */
    private String id = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        setContentView(R.layout.activity_er_wei_ma);
        id = getIntent().getStringExtra("id");
        ButterKnife.bind(this);
        initData();
    }

    private void initData() {
        //二维码本地存放地址
        mErWeiMaFilePath = Utils.getAppFilePath(mContext) + File.separator
                + "qr_" + System.currentTimeMillis() + ".jpg";
        //二维码中的内容
        LogUtils.e("生成二维码的地址：" + GlobalUrl.ERWEIMA_URL + id);
        boolean success = Utils.createQRImage(GlobalUrl.ERWEIMA_URL + id,
                Utils.dp2px(mContext, 165),
                Utils.dp2px(mContext, 165),
                null,
                mErWeiMaFilePath);
        if (success) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    ivErweima.setImageBitmap(BitmapFactory.decodeFile(mErWeiMaFilePath));
                }
            });
        }
    }

    @OnClick({R.id.ll_erweima})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_erweima:
                JumpHelper.finish(mContext);
                break;
        }
    }
}
